package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;
    /*@GetMapping
    public Result page(Integer page, Integer pageSize,
                       String name, Integer  gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("分页查询,参数:{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        PageResult<Emp> pageResult = empService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageResult);
    }*/
    @GetMapping
    public Result page(EmpQueryParam param){
        log.info("分页查询,参数:{}",param);
        PageResult<Emp> pageResult = empService.page(param);
        return Result.success(pageResult);
    }
    @DeleteMapping
    public Result delete(Integer[] ids){
        log.info("删除员工,参数:{}", Arrays.toString(ids));
        empService.delete(ids);
        return Result.success();
    }
    @PostMapping
    public Result add(@RequestBody Emp emp ){
        log.info("添加员工,参数:{}",emp);
        empService.add(emp);
        return Result.success();
    }
    @GetMapping ("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("查询id为{}的员工数据",id);
        Emp emp = empService.getInfo(id);
        return Result.success(emp);
    }
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工,参数:{}",emp);
        empService.update(emp);
        return Result.success();
    }
    @GetMapping("list")
    public Result list(){
        log.info("查询所有员工数据");
        List<Emp> empList = empService.list();
        return Result.success(empList);
    }
}
